<?php
    //
    header("content-type:text/html;charset=utf-8");

    session_start();
    $link = @mysqli_connect('localhost','root','root','db_info');
    if(empty($link)){
        echo '数据库连接失败';
        die();
    }
    mysqli_query($link,"set names 'utf8'");
    date_default_timezone_set("PRC");
    $data = $_POST;
    include_once 'db.php';

    switch ($data['type']){
        case 'login':
            login();
            break;
        case 'logout':
            logout();
            break;
        case 'register':
            register();
            break;
        case 'checkusername':
            checkusername();
            break;
        case 'newSub':
            newSub();
            break;
        default :
            echo '<h1 style="text-align: center;">参数错误</h1>';
            die();
            break;
    }


    //登录 返回状态： 1 = 登录成功 0 = 登录失败
    function login(){
        global $data;
        $user = getList('user','username = "'.$data['username'].'"');
        $username = $data['username'];
        $password = md5($data['password']);
//        die();
        if(empty($user)){
            echo 0;
            die();
        }
        $currentUsername = $user[0]['username'];
        $currentPassword = $user[0]['password'];

        if($currentUsername === $username && $currentPassword === $password){
            $_SESSION['username'] = $currentUsername;
            $_SESSION['uid'] = $user[0]['uid'];
            echo 1;
        }else{
            echo 0;
        }
    }
    // 注册 返回状态： 4 = 没有输入用户名  3 = 已有用户名  2 = 密码与重复密码不一致  1 = 成功  0 = 未定义的意外错误
    function register(){
        global $data;
        $username = $data['username'];
        $password = $data['password'];
        $rePassword = $data['rePassword'];
        $email = $data['email'];

        if($username === ''){
            echo 4;
            die();
        }

        $onlyOne = getOne('user',"username = '".$username."'");
//        echo $onlyOne;
//        die();
        if(!empty($onlyOne)){
            echo 3;
            die();
        }

        if($password !== $rePassword){
            echo 2;
            die();
        }

        //检查用户名唯一性

        $result = add('user',[
            'username' => $username,
            'password' => md5($password),
            'email' => $email,
        ]);
        if($result){
            echo 1;
        }else{
            echo 0;
        }
    }


    //检查用户名唯一性
    function checkusername(){
        global $data;
        $username = $data['username'];
        $onlyOne = getOne('user',"username = '".$username."'");
        //        echo $onlyOne;
        //        die();
        if(!empty($onlyOne)){
            echo 1;
        }else{
            echo 0;
        }
    }

    function newSub(){
        global $data;
        $email = $data['email'];
        $res = add('submit',[
            'email' => $email
        ]);
        if(empty($res)){
            echo 0;
        }else{
            echo 1;
        }
    }
    //注销
    function logout(){
        session_destroy();
        echo 1;
    }